package org.kk.cordova.uninstall;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

public class Uninstall extends CordovaPlugin {
	public static final String ERROR_WX_NOT_INSTALLED = "Not installed";

	/**
	 * Constructor.
	 */
	public Uninstall() {
	}

	/**
	 * Sets the context of the Command. This can then be used to do things like
	 * get file paths associated with the Activity.
	 *
	 * @param cordova
	 *            The context of the main Activity.
	 * @param webView
	 *            The CordovaWebView Cordova is running in.
	 */
	public void initialize(CordovaInterface cordova, CordovaWebView webView) {
		super.initialize(cordova, webView);
	}

	/**
	 * Executes the request and returns PluginResult.
	 *
	 * @param action
	 *            The action to execute.
	 * @param args
	 *            JSONArry of arguments for the plugin.
	 * @param callbackContext
	 *            The callback id used when calling back into JavaScript.
	 * @return True if the action was valid, false if not.
	 */
	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
		if (action.equals("remove")) {
			return remove(args, callbackContext);
		} else {
			if (action.equals("isInstalled")) {
				return isInstalled(args, callbackContext);
			}
		}
		return super.execute(action, args, callbackContext);
	}

	// --------------------------------------------------------------------------
	// LOCAL METHODS
	// --------------------------------------------------------------------------

	/**
	 * Get the OS name.
	 * 
	 * @return
	 */
	public boolean remove(JSONArray args, CallbackContext callbackContext) {
		String packageName;
		try {
			packageName = args.getString(0);
			Uri uri = Uri.parse("package:" + packageName);// 获取删除包名的URI
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			intent.setAction(Intent.ACTION_DELETE);// 设置我们要执行的卸载动作
			intent.setData(uri);// 设置获取到的URI
			cordova.startActivityForResult(this, intent, 1);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		;

		return true;
	}

	/**
	 * Get the device's Universally Unique Identifier (UUID).
	 *
	 * @return
	 */

	public boolean isInstalled(JSONArray args, CallbackContext callbackContext) {
		String packageName = null;
		try {
			packageName = args.getString(0);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try

		{
			ApplicationInfo info = cordova.getActivity().getPackageManager().getApplicationInfo(packageName,
					PackageManager.GET_UNINSTALLED_PACKAGES);
			callbackContext.success("true");
			return true;
		}

		catch (NameNotFoundException e)

		{
			callbackContext.error(ERROR_WX_NOT_INSTALLED);
			return false;

		}
	}

}
